package com.higgins.client.tabs;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.client.GreetingService;
import com.higgins.client.GreetingServiceAsync;
import com.higgins.client.components.RecipientColumns;
import com.higgins.client.components.RecipientTable;
import com.higgins.client.components.StrategyColumns;
import com.higgins.client.components.StrategyTable;
import com.higgins.client.dataModel.CoveringLetterDTO;
import com.higgins.client.dataModel.RegionDTO;
import com.higgins.client.dataModel.SectorDTO;
import com.higgins.client.dataModel.StrategyDTO;

public class RecipientTab extends VerticalPanel {
	ListBox strategyListBox = new ListBox();
	ListBox regionListBox = new ListBox();
	ListBox sectorListBox = new ListBox();
	List<StrategyDTO> strategyList = null;
	List<RegionDTO> regionList = null;
	List<SectorDTO> sectorList = null;
	private boolean firstRun = true;
	private RecipientTable recipientTable;
    private HorizontalPanel rowPanel = new HorizontalPanel();
	private final GreetingServiceAsync greetingService = GWT
				.create(GreetingService.class);
	
	public RecipientTab()	{
	}
	public void update()	{
		if (firstRun)
			initialise();
		else
			refresh();
	}
		
	public void initialise()	{
		firstRun = false;
        rowPanel.setSpacing(3);
        add(rowPanel);

		loadStrategyListBox();
		loadRegionListBox();
		loadSectorListBox();
		attachStrategyListBoxHandler();
		attachRegionListBoxHandler();
		attachSectorListBoxHandler();
	}
	public void refresh()	{
	}
	private void loadStrategyListBox()	{
		rowPanel.add(strategyListBox);
		greetingService.retrieveStrategys(new AsyncCallback<List<StrategyDTO>>()	{
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<StrategyDTO> result) {
				strategyList = result;
				for (StrategyDTO dto:result)	{
					strategyListBox.addItem(dto.getName());
				}
				strategyListBox.setSelectedIndex(0);
				checkLoadComplete();
			}});
	}
	private void loadRegionListBox()	{
		rowPanel.add(regionListBox);
		greetingService.retrieveRegions(new AsyncCallback<List<RegionDTO>>()	{
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<RegionDTO> result) {
				regionList = result;
				for (RegionDTO dto:result)	{
					regionListBox.addItem(dto.getName());
				}
				regionListBox.setSelectedIndex(0);
				checkLoadComplete();
			}});
		
	}
	private void loadSectorListBox()	{
		rowPanel.add(sectorListBox);
		greetingService.retrieveSectors(new AsyncCallback<List<SectorDTO>>()	{
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<SectorDTO> result) {
				sectorList = result;
				for (SectorDTO dto:result)	{
					sectorListBox.addItem(dto.getName());
				}
				sectorListBox.setSelectedIndex(0);
				checkLoadComplete();
			}});
		
	}
	private void checkLoadComplete()	{
		if (strategyListBox.getItemCount()>0 && regionListBox.getItemCount()>0 && sectorListBox.getItemCount()>0)
			initialiseTable();
	}
	private void initialiseTable()	{
		RecipientColumns recipientColumns = new RecipientColumns();
		recipientTable = new RecipientTable(recipientColumns);
		recipientTable.update(strategyList.get(strategyListBox.getSelectedIndex()),
							regionList.get(regionListBox.getSelectedIndex()), 
							sectorList.get(sectorListBox.getSelectedIndex()));
		add(recipientTable);
	}
	private void updateTable()	{
		recipientTable.update(strategyList.get(strategyListBox.getSelectedIndex()),
				regionList.get(regionListBox.getSelectedIndex()), 
				sectorList.get(sectorListBox.getSelectedIndex()));
	}
	private void attachStrategyListBoxHandler()	{
		strategyListBox.addChangeHandler(new ChangeHandler()  {

			@Override
			public void onChange(ChangeEvent event) {
				updateTable();
			}
			
		});
	}	private void attachRegionListBoxHandler()	{
		regionListBox.addChangeHandler(new ChangeHandler()  {

			@Override
			public void onChange(ChangeEvent event) {
				updateTable();
			}
			
		});
	}
	private void attachSectorListBoxHandler()	{
		sectorListBox.addChangeHandler(new ChangeHandler()  {

			@Override
			public void onChange(ChangeEvent event) {
				updateTable();
			}
			
		});
		
	}

}
